package com.tedu.admin.service.impl;

import com.tedu.common.exception.ServiceException;
import com.tedu.common.response.StatusCode;
import com.tedu.admin.mapper.AdminMapper;
import com.tedu.admin.pojo.dto.AdminLoginParam;
import com.tedu.admin.pojo.dto.AdminQuery;
import com.tedu.admin.pojo.dto.AdminSaveParam;
import com.tedu.admin.pojo.entity.Admin;
import com.tedu.admin.pojo.vo.AdminVO;
import com.tedu.admin.service.AdminService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;

    @Override
    public AdminVO login(AdminLoginParam adminLoginParam) {
        log.debug("管理员登录业务参数：{}", adminLoginParam);
        AdminVO adminVO = adminMapper.selectByAdminname(adminLoginParam.getAdminname());
        if (adminVO == null) {
            //如果主业务遇到了问题，需要抛出业务层异常，此时的异常结果由全局异常处理器返回给前端
            //因为业务层不能直接和前端打交道，只能Controller层
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if (!adminVO.getPassword().equals(adminLoginParam.getPassword())) {
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }
        //想要看是否有数据，可以用日志打印查看
        log.debug("管理员查询结果：{}", adminVO);
        return adminVO;
    }

    @Override
    public void save(AdminSaveParam adminSaveParam) {
        log.debug("保存管理员业务：{}", adminSaveParam);
        Admin admin = new Admin();
        BeanUtils.copyProperties(adminSaveParam, admin);
        if (admin.getId() == null) {//新增
            admin.setPassword("123456");
            admin.setCreateTime(new Date());
            adminMapper.insertAdmin(admin);
        } else {//更新
            admin.setUpdateTime(new Date());
            adminMapper.update(admin);
        }
    }

    @Override
    public List<AdminVO> selectAdmin(AdminQuery adminQuery) {
        log.debug("查询管理员业务：{}", adminQuery);
        List<AdminVO> list = adminMapper.selectAdmin(adminQuery);
        return list;
    }

    @Override
    public void resetPassword(Long adminId) {
        log.debug("重置密码业务：{}", adminId);
        Admin admin = new Admin();
        //id是主键不可变的，这里setId通常是定位作用，告诉数据库更新哪个管理员的记录
        admin.setId(adminId);
        admin.setPassword("root");
        admin.setUpdateTime(new Date());
        adminMapper.update(admin);
    }

    @Override
    public void updateStatus(Long adminId, String status) {
        log.debug("修改管理员状态业务：{}，{}", adminId, status);
        Admin admin = new Admin();
        admin.setId(adminId);
        admin.setStatus(status);
        admin.setUpdateTime(new Date());
        adminMapper.update(admin);
    }

    @Override
    public void deleteAdmin(Long adminId) {
        log.debug("删除管理员业务：{}", adminId);
        adminMapper.delete(adminId);
    }

    @Override
    public List<AdminVO> selectAuditList(Long parentId) {
        log.debug("查询审批人列表业务：{}",parentId);
        //1.准备一个空集合,用来保存查出来的多个审批人
        ArrayList<AdminVO> adminVOList = new ArrayList<>();
        //2.先根据传过来的直属领导id查出直属领导
        AdminVO auditAdmin1 = adminMapper.selectById(parentId);
        adminVOList.add(auditAdmin1);
        //3.如果有直属领导,且直属领导还有上级
        if(auditAdmin1 != null && auditAdmin1.getParentId() != null){
            //4.再根据直属领导的领导id将上上级查出来
            AdminVO auditAdmin2 = adminMapper.selectById(auditAdmin1.getParentId());
            adminVOList.add(auditAdmin2);
        }
        //5.将准备好的审批人列表返回给上一层
        return adminVOList;
    }
}
